﻿@page "/keepalivedConfig"

<MContainer>
    <MCard>
        <MCardTitle>
            <span class="text-h5">Keepalived 配置管理</span>
            <MSpacer />
            <MButton Color="primary" 
            OnClick="LoadConfig"
            Loading="_isLoading">
                <MIcon Left>mdi-refresh</MIcon>
                刷新配置
            </MButton>
        </MCardTitle>

        <MTabs @bind-Value="_activeTab">
            <MTab Value="1">
                <MIcon Left>mdi-server</MIcon>
                全局配置
            </MTab>
            <MTab Value="2">
                <MIcon Left>mdi-redundant-box</MIcon>
                VRRP实例
            </MTab>
            <MTab Value="3">
                <MIcon Left>mdi-load-balancer</MIcon>
                虚拟服务器
            </MTab>
            <MTab Value="4">
                <MIcon Left>mdi-script-text</MIcon>
                跟踪脚本
            </MTab>
        </MTabs>

        <MTabsItems Value="_activeTab">
            <MTabItem Value="1">
                <GlobalConfigEditor @bind-Model="_config.GlobalDefs" />
            </MTabItem>

            <MTabItem Value="2">
                <VrrpInstanceList @bind-Items="_config.VrrpInstances"
                                  TItem="VrrpInstance"
                OnAdd="AddVrrpInstance"
                OnRemove="RemoveVrrpInstance" />
            </MTabItem>

            <MTabItem Value="3">
                <VirtualServerList @bind-Items="_config.VirtualServers"
                                   TItem="VirtualServer"
                OnAdd="AddVirtualServer"
                OnRemove="RemoveVirtualServer" />
            </MTabItem>

            <MTabItem Value="4">
                <VrrpScriptList @bind-Items="_config.Scripts"
                                TItem="VrrpScript"
                OnAdd="AddVrrpScript"
                OnRemove="RemoveVrrpScript" />
            </MTabItem>
        </MTabsItems>

        <MCardActions>
            <MSpacer />
            <MButton Color="primary" 
            OnClick="SaveConfig"
            Loading="_isSaving">
                <MIcon Left>mdi-content-save</MIcon>
                保存配置
            </MButton>
        </MCardActions>
    </MCard>
</MContainer>

@code {
    [Inject]
    public IKeepalivedConfigRepository Repository{ get; set; }
    [Inject]
    public IRemoteKeepAlivedService KeepAlivedService { get; set; }
    [Inject]
    public IPopupService PopupService { get; set; }
    private KeepalivedConfig _config = new();
    private StringNumber _activeTab = "1";
    private bool _isLoading = false;
    private bool _isSaving = false;

    protected override async Task OnInitializedAsync()
    {
        await LoadConfig();
    }

    private async Task LoadConfig()
    {
        _isLoading = true;
        try
        {
            _config = KeepAlivedService.GetKeepalivedConfig();
            PopupService.EnqueueSnackbarAsync("配置加载成功",AlertTypes.Success);
        }
        catch (Exception ex)
        {
            PopupService.EnqueueSnackbarAsync($"加载配置失败: {ex.Message}", AlertTypes.Error);
        }
        finally
        {
            _isLoading = false;
        }
    }

    private async Task SaveConfig()
    {
        _isSaving = true;
        try
        {
            await KeepAlivedService.UpdateKeepalivedConfig(_config);
            PopupService.EnqueueSnackbarAsync("配置保存成功", AlertTypes.Success);
        }
        catch (Exception ex)
        {
            PopupService.EnqueueSnackbarAsync($"保存配置失败: {ex.Message}", AlertTypes.Success);
        }
        finally
        {
            _isSaving = false;
        }
    }

    private void AddVrrpInstance()
    {
        _config.VrrpInstances.Add(new VrrpInstance
        {
            Name = $"VI_{_config.VrrpInstances.Count + 1}",
            VirtualRouterId = 50 + _config.VrrpInstances.Count + 1,
            Authentication = new VrrpAuthentication()
        });
    }

    private void RemoveVrrpInstance(VrrpInstance instance)
    {
        _config.VrrpInstances.Remove(instance);
    }

    private void AddVirtualServer()
    {
        _config.VirtualServers.Add(new VirtualServer
        {
            IpAddress = "192.168.1.100",
            Port = 80,
            HealthCheck = new TcpHealthCheck { ConnectPort = 80 }
        });
    }

    private void RemoveVirtualServer(VirtualServer server)
    {
        _config.VirtualServers.Remove(server);
    }

    private void AddVrrpScript()
    {
        _config.Scripts.Add(new VrrpScript
        {
            Name = $"script_{_config.Scripts.Count + 1}",
            Interval = 2,
            Weight = 2
        });
    }

    private void RemoveVrrpScript(VrrpScript script)
    {
        _config.Scripts.Remove(script);
    }
}